<?php 

require("../include/config.php");
require("../API/php/fonctions.php");
require("../API/php/header.php");

$modeDebug = false;

// phpsql/mysql_getTicketsSupDeb.php?milis=123456789&ctrl=ok&code_user=HNA

//[x][0]id_toc_ori 
//[x][1]id_toc 
//[x][2]revision 
//[x][3]responsable 
//[x][4]date_creation 
//[x][5]date_statut 
//[x][6]libelle
//[x][7]sn3 
//[x][8]code_user 
//[x][9]date_creation
//[x][10]date_modif

// IN obligatoire
$arrayGet = array(
    "ctrl" => null,
    "code_user" => null
);

$arrayValeur = recupGet($arrayGet, $bolDecode);

if($arrayValeur["error"] == null){
	// CONNECION SQL
	$db = mysql_connect($host, $base, $mdp)  or die('Erreur de connexion '.mysql_error());
        mysql_query("SET NAMES 'utf8'");

	// CONNECION BASE
	mysql_select_db($base,$db)  or die('Erreur de selection '.mysql_error());
        
        $strFiltreResponsable = "";
        if($arrayValeur["code_user"] != ""){
            $strFiltreResponsable = " AND responsable = '".$arrayValeur["code_user"]."'";
        }
	
	$sql = "SELECT 
            a.id_toc_ori, 
            a.id_toc, 
            a.revision, 
            a.responsable, 
            a.date_creation, 
            a.date_statut, 
            a.libelle, 
            b.sn3, 
            b.code_user, 
            b.date_creation, 
            b.date_modif
            FROM `".$prefixTable."vm_inventaire_tocs` a
            LEFT JOIN `".$prefixTable."tab_tickets_sup_deb` b
                ON a.id_toc = b.id_toc AND a.revision = b.revision
            WHERE 1=1
            AND a.statut = 'Support/Déblocage'
            AND a.emis != 'oui'
            AND a.responsable in (SELECT b.responsable FROM `effectifs` b WHERE 1=1
                    AND b.date_in != '0000-00-00' 
                    AND b.date_out != '0000-00-00')
            AND NOT EXISTS (select 1 from vm_inventaire_tocs_vsr z WHERE z.id_toc = a.id_toc)
            ".$strFiltreResponsable."
            ORDER BY responsable, id_toc
	;";

	$req = mysql_query($sql) or die('Erreur SQL !<br>'.$sql.'<br>'.mysql_error()); 
	
	// on fait une boucle qui va faire un tour pour chaque enregistrement 
	while($data = mysql_fetch_assoc($req)) 
	{ 
            $strSorti .= $data['id_toc_ori']
                .'&'.$data['id_toc']
                .'&'.$data['revision']
                .'&'.$data['responsable']
                .'&'.$data['date_creation']
                .'&'.$data['date_statut']
                .'&'.$data['libelle']
                .'&'.$data['sn3']
                .'&'.$data['code_user']
                .'&'.$data['date_creation']
                .'&'.$data['date_modif']
                .'&'
                ."\r\n"; 
        }
        
	// DECONNECION SQL
	mysql_close();
	
	if($modeDebug){
		$strSorti .= ('<br><br><br><br>'.$sql);
	}
}else{
	$strSorti = "ERROR:".$arrayValeur["error"];
}

//require("../API/php/footer.php");

echo $strSorti;
?>